LINQ to XML - Generating an SGML Document


LINQ, LINQ to XML, .NET Framework 4.0
LINQ, LINQ to XML, XML, Console Window, XML Documentation, SGML
Desktop, Data
en-US
6/28/2011

Introduction

These samples demonstrate how to work with XmlReaderSettings and DtdProcessing to output an SGML document.

Building the Sample

  1. Open Program.cs
  2. Uncomment the line for the sample that you want to run; comment ones you don't want to run
  3. Press Ctrl + F5

Screenshot

Sample Code

C#
Edit|Remove
            [Category("SGML - Standard Generalized Markup Language")] 
            [Description(@"Locate all paragraphs in the report (all ""para"" elements occurring anywhere within the ""report"" element).")] 
            public void Q42() 
            { 
                XDocument sgml = null; 
                // XDocument load by default does not resolve external entities so passing in our 
                // own reader with the correct setting. 
                XmlReaderSettings rs = new XmlReaderSettings(); 
                rs.DtdProcessing = DtdProcessing.Parse; 
                using (XmlReader r = XmlReader.Create("sgml.xml", rs)) 
                { 
                    sgml = XDocument.Load(r); 
                } 
 
                var result = new XElement("result", 
                                 sgml.Descendants("report") 
                                     .Descendants("para")); 
 
                Console.WriteLine(result); 
 
                // Solution in XQuery: 
                // <result> 
                // {  
                //    doc(""sgml.xml"")//report//para  
                // } 
                // </result> 
            }

Source Code Files

More Information

For more information on LINQ to XML: http://msdn.microsoft.com/en-us/library/bb387098.aspx